package com.hare.harelearn.TempLearn;

import com.alibaba.fastjson.JSON;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @program: HareLearn
 * @description:
 * @author: Hare
 * @create: 2025−02-22 16:01
 * @Version 1.0
 **/
public class StreamLearn {
    public static void main(String[] args) {
        List<Integer> l = Arrays.asList(1,2);
        te(l);
        System.out.println(l);
        List<String> list = new ArrayList<>();
        list.add("java");
        list.add("python");
        list.add("vue");
        list.add("go");

        // 根据字符串长度排序
        List<String> collect = list.stream().sorted(Comparator.comparing(String::length).reversed()).collect(Collectors.toList());
        System.out.println(Arrays.toString(collect.toArray()));

        List<String> strList = new ArrayList<>();
        List<String> strList1 = new ArrayList<>();
        strList.add("java");
        strList.add("python");

        strList1.add("vue");
        strList1.add("go");
        List<String> collect1 = Stream.of(strList, strList1).flatMap(List::stream).collect(Collectors.toList());
        long count = collect1.stream().count();
        System.out.println("统计list元素个数:"+count);

        List<String> collect2 = Stream.of(strList, strList1).flatMap(List::stream).filter(item -> item.length() > 2).collect(Collectors.toList());
        // 输出filter过滤后的数据
        collect2.stream().forEach(System.out::println);

        List<Integer> inList  = List.of(1,2,3,4,5,6,5,6,10);
        Integer reduce1 = inList.stream().reduce(0, Integer::sum);
        System.out.println("list集合元素求和:"+reduce1+"\n");

        // 过滤
        inList.stream().distinct().collect(Collectors.toList()).stream().forEach(System.out::print);

        // 截取
        List<Integer> collect3 = inList.stream().limit(2).collect(Collectors.toList());
        System.out.println("\n"+Arrays.toString(collect3.toArray()));
        // 跳过
        List<Integer> collect4 = inList.stream().skip(2).collect(Collectors.toList());
        System.out.println(Arrays.toString(collect4.toArray()));

        List<Student> listStu = new ArrayList<>();
        listStu.add(new Student("张三", 18, "女"));
        listStu.add(new Student("李四", 19, "女"));
        listStu.add(new Student("王五", 20, "女"));
        listStu.add(new Student("赵六", 21, "女"));
        listStu.add(new Student("钱七", 22, "女"));
        listStu.add(new Student("孙八", 23, "女"));
        listStu.add(new Student("周九", 24, "女"));
        listStu.add(new Student("吴十", 25, "男"));
        listStu.add(new Student("郑十一", 26, "男"));
        listStu.add(new Student("王十二", 27, "男"));
        listStu.add(new Student("冯十三", 28, "男"));
        listStu.add(new Student("陈十四", 29, "男"));
        listStu.add(new Student("褚十五", 30, "男"));
        listStu.add(new Student("卫十六", 31, "男"));
        listStu.add(new Student("蒋十七", 32, "男"));

        Integer reduce = listStu.stream().map(Student::getAge).reduce(0, Integer::sum);
        System.out.println("学生年龄总和:"+reduce);

        List<Integer> collect5 = listStu.stream().map(Student::getAge).collect(Collectors.toList());
        System.out.println("学生年龄集合:"+Arrays.toString(collect5.toArray()));

        Map<String, List<Student>> collect6 = listStu.stream().collect(Collectors.groupingBy(Student::getSex));

        System.out.println(JSON.toJSONString(collect6));
    }

    static void te(List<Integer> l){
        l.add(3);

    }
    // 学生类
    static class Student {
        private String name;
        private int age;
        private String sex;

        public Student(String name, int age, String sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }
    }
}
